package com.wl.application.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.application.common.all.Result;
import com.wl.application.entity.Dept;
import com.wl.application.entity.vo.DeptVo;
import com.wl.application.services.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wl
 * @version 1.0
 * @date 2022/6/18
 * @description:
 */
@RestController
@RequestMapping("/dept")

public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/list")
    public Result list() {
        List<Dept> list = deptService.list();
        return Result.ok(list);
    }

    @PostMapping("/page")
    public Result page(@RequestBody Page<Dept> page){
        Page<Dept> page1 = deptService.page(page);
        return Result.ok((page1));
    }

    @GetMapping("/checkDno/{deptno}")
    public Result checkDno(@PathVariable("deptno") Integer dno) {
        Dept byId = deptService.getById(dno);
        String msg="部门已存在!";
        if (byId != null) {
           return Result.ok(msg);
        }
        return Result.ok("");
    }

    @PostMapping("/addDept")
    public Result addDept(@RequestBody Dept dept){
        boolean save = deptService.save(dept);
        return Result.ok("添加成功!");
    }

    @DeleteMapping("/delete/{dno}")
    public Result deleteDeptForDno(@PathVariable("dno") Integer dno){
        boolean b = deptService.removeById(dno);
        return Result.ok("删除成功!");
    }


    @DeleteMapping("/deleteCheckAll")
    public Result deleteCheckAll(@RequestBody List<Integer> dnoList){
        boolean b = deptService.removeBatchByIds(dnoList);
        return Result.ok("删除成功!");
    }
    @GetMapping("/getOne/{dno}")
    public Result getOne(@PathVariable("dno") Integer dno){
        Dept byId = deptService.getById(dno);
        return Result.ok(byId);
    }

    @PutMapping("/update")
    public Result updateDept(@RequestBody Dept dept){
        boolean b = deptService.updateById(dept);
        return Result.ok("修改成功!");
    }

    /**
     * 查询部门信息和人数
     */
    @GetMapping("/DeptAndCount")
    public Result getDeptAndNum(){
        List<DeptVo> deptAndNum = deptService.getDeptAndNum();
        return Result.ok(deptAndNum);
    }
}
